import { modalSlideUpAtomsFamily as atomsFamily } from "@/components/composite/ModalSlideUp/main"
import { useAtomValue, useSetAtom } from "jotai"

export function useModalSlideUp(name: Components.WhereIsTheModalSlideUp = "root") {
    const atoms = useAtomValue(atomsFamily(name))
    const state = useAtomValue(atoms.stateAtom)
    const open = useSetAtom(atoms.openAtom)
    const close = useSetAtom(atoms.closeAtom)
    // 包裝 open 函數，支持傳入 activeId
    const openWithId = (
        element: React.ReactNode,
        activeId?: string,
        opts?: Parameters<typeof open>[1],
    ) => {
        return open(element, { ...opts, activeId })
    }

    return {
        state,
        open,
        openWithId,
        close,
    }
}
